import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import { loadEnv } from "vite";
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// https://vitejs.dev/config/
export default ({ mode }) => {
  return defineConfig({
    plugins: [vue(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons/svg")],
        // 指定symbolId格式
        symbolId: "icon-[dir]-[name]",
        /**
         * 自定义插入位置
         * @default: body-last
         */
        // inject?: 'body-last' | 'body-first'
        /**
         * custom dom id
         * @default: __svg__icons__dom__
         */
        // customDomId: '__svg__icons__dom__',
      })
],
    resolve: {
      alias: {
        "@": path.resolve("./src"), // 相对路径别名配置，使用 @ 代替 src
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/styles/constant.scss";',
        },
      },
    },
    server: {
      open: true, //启动项目自动弹出浏览器
      port: 4000, //启动端口
      proxy: {
        "/api": {
          target: loadEnv(mode, process.cwd()).VITE_APP_URL, //实际请求地址
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
    build: {
      //打包方式选择terser(在生产环境中会清除打印)
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
    }
  });
};
